package com.chuanyang.driverh.mainother;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviStaticInfo;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.autonavi.tbt.NaviStaticInfo;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.SpeedUtil;

import java.util.ArrayList;
import java.util.List;

public class GpsNaviActivity extends Activity implements AMapNaviListener, AMapNaviViewListener {
    NaviLatLng mEndLatlng;
    NaviLatLng mStartLatlng;
    private AMapNavi mAMapNavi;
    List<NaviLatLng> mStartList = new ArrayList<NaviLatLng>();
    List<NaviLatLng> mEndList = new ArrayList<NaviLatLng>();
    List<NaviLatLng> mWayPointList;
    AMapNaviView mAMapNaviView;
//    private String startLongtutide;
//    private String startLatutide;
    private double endlongtitude;
    private double endlatutide;

    private boolean isCongestion;
    private boolean isCost;
    private boolean isHightspeed;
    private boolean isAvoidhightspeed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.other_act_basic_navi);
        getData();

        mAMapNavi = AMapNavi.getInstance(getApplicationContext());
        mAMapNavi.addAMapNaviListener(this);
        mAMapNavi.setEmulatorNaviSpeed(75);

        mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
        mAMapNaviView.onCreate(savedInstanceState);
        mAMapNaviView.setAMapNaviViewListener(this);

    }

    private void getData() {
        //获取传递过来的终点数据和当前的位置
        Intent intent = getIntent();
        endlongtitude = intent.getDoubleExtra("endlongtitude", 0);
        endlatutide = intent.getDoubleExtra("endlatitude", 0);
        mStartLatlng = new NaviLatLng(BaseApplication.getInstance().getLocationEntity().getLatitude(), BaseApplication.getInstance().getLocationEntity().getLongitude());
        mEndLatlng = new NaviLatLng(endlatutide,endlongtitude);

        mStartList.add(mStartLatlng);
        mEndList.add(mEndLatlng);
        isCongestion = SharedPrefUtil.isCongestion();
        //避免收费
        isCost = SharedPrefUtil.isCost();
        //高速优先
        isHightspeed = SharedPrefUtil.isHightspeed();
        //避免高速
        isAvoidhightspeed = SharedPrefUtil.isAvoidhightspeed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAMapNaviView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAMapNaviView.onPause();

//        仅仅是停止你当前在说的这句话，一会到新的路口还是会再说的
        SpeedUtil.stopSpeaking();
//
//        停止导航之后，会触及底层stop，然后就不会再有回调了，但是讯飞当前还是没有说完的半句话还是会说完
//        mAMapNavi.stopNavi();
    }

    //    /**
//     * 如果使用无起点算路，请这样写
//     */
//    private void noStartCalculate() {
//        //无起点算路须知：
//        //AMapNavi在构造的时候，会startGPS，但是GPS启动需要一定时间
//        //在刚构造好AMapNavi类之后立刻进行无起点算路，会立刻返回false
//        //给人造成一种等待很久，依然没有算路成功 算路失败回调的错觉
//        //因此，建议，提前获得AMapNavi对象实例，并判断GPS是否准备就绪
//
//
//        if (mAMapNavi.isGpsReady())
//           // mAMapNavi.calculateDriveRoute(mEndList, mWayPointList, PathPlanningStrategy.DRIVING_DEFAULT);
//            mAMapNavi.calculateDriveRoute(mEndList, mWayPointList, PathPlanningStrategy.DRIVING_DEFAULT);
//    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAMapNaviView.onDestroy();
        //since 1.6.0 不再在naviview destroy的时候自动执行AMapNavi.stopNavi();请自行执行
        mAMapNavi.stopNavi();
        mAMapNavi.destroy();
        SpeedUtil.destroy();
    }


    @Override
    public void onInitNaviFailure() {
        LogUtils.e("onInitNaviFailure","onInitNaviFailure--------");

    }

    @Override
    public void onInitNaviSuccess() {
        LogUtils.e("onInitNaviSuccess","onInitNaviSuccess--------");

        //初始化成功

//        12-规避拥堵且不走收费道路
//        static int 	DRIVING_DEFAULT
//        0-默认算路模式 速度优先（常规最快） 速度优先（常规默认选项），是指不考虑任何策略的情况下，理想路况下，根据道路固有属性（静态权值）计算得到的路线。
//        static int 	DRIVING_FASTEST_TIME
//        4-时间优先 规避拥堵的路线（考虑实时路况） 躲避拥堵，完全可以按照字面意思理解，即现实路况下（结合实时交通路况），计算出最优路线。
//        static int 	DRIVING_MULTIPLE_ROUTES
//        13-多路径算路
//        static int 	DRIVING_NO_EXPRESS_WAYS
//        3-普通路优先（不走快速路，包含高速路）
//        static int 	DRIVING_SAVE_MONEY
//        1-费用优先（尽量避开收费道路）
//        static int 	DRIVING_SHORT_DISTANCE
//        2-距离优先（距离最短）
        int strategy = 0;
        try
        {
            strategy = mAMapNavi.strategyConvert(isCongestion, isAvoidhightspeed, isCost, isHightspeed, false);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
//        LogUtils.e("参数", startLongtutide + "-----" + startLatutide + "---" + endlongtitude + "----" + endlatutide);
        mAMapNavi.calculateDriveRoute(mStartList, mEndList, mWayPointList, strategy);
    }
    @Override
    public void onStartNavi(int i) {
        //开始导航回调
    }

    @Override
    public void onArriveDestination() {
        // TODO Auto-generated method stub
        SpeedUtil.broadcastOrder(this, "到达目的地");
    }

    @Override
    public void onArriveDestination(NaviStaticInfo naviStaticInfo) {

    }

    @Override
    public void onArriveDestination(AMapNaviStaticInfo aMapNaviStaticInfo) {

    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {
        //当前位置回调
        LogUtils.e("location", aMapNaviLocation.getAltitude().toString());
    }

    @Override
    public void onGetNavigationText(int i, String s) {          //播报类型和播报文字回调


        SpeedUtil.broadcastOrder(this, s);
    }

    @Override
    public void onEndEmulatorNavi() {

        SpeedUtil.broadcastOrder(this, "导航结束");
    }


    @Override
    public void onCalculateRouteSuccess() {
        mAMapNavi.startNavi(NaviType.GPS);
        //  mAMapNavi.startNavi(NaviType.EMULATOR);

    }

    @Override
    public void onCalculateRouteFailure(int i) {
        SpeedUtil.broadcastOrder(this, "路径计算失败，请检查网络或输入参数");
    }

    @Override
    public void onReCalculateRouteForYaw() {
        //偏航后重新计算路线回调


    }

    //驾车导航时，如果前方遇到拥堵时需要重新计算路径的回调。
    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int i) {
        //到达途径点

    }

    //用户手机GPS设置是否开启的回调函数。
    @Override
    public void onGpsOpenStatus(boolean b) {
        if (!b) {
            PrintUtil.toast(this, "请打开GPS");
        }

    }

    @Override
    public void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {
        //导航过程中的信息更新，请看NaviInfo的具体说明

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {
        //已过时

    }

    @Override
    public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {
        //已过时

    }

//    @Override
//    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {
//        //更新交通设施信息
//
//    }
//
//    @Override
//    public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {
//
//    }


    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {
        //显示转弯回调

    }

    @Override
    public void hideCross() {
        //隐藏转弯回调

    }

    @Override
    public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {
        //显示车道信息

    }

    @Override
    public void hideLaneInfo() {
        //隐藏车道信息

    }

    @Override
    public void onCalculateMultipleRoutesSuccess(int[] ints) {//多路径规划成功

    }

    @Override
    public void notifyParallelRoad(int i) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {
        //更新交通设施信息

    }

    @Override
    public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {
        //更新巡航模式的统计信息

    }

    @Override
    public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {
        //更新巡航模式的拥堵信息

    }

    @Override
    public void onNaviSetting() {

    }

    @Override
    public void onNaviCancel() {
          finish();
    }

    @Override
    public boolean onNaviBackClick() {

        return false;
    }

    @Override
    public void onNaviMapMode(int i) {

    }

    @Override
    public void onNaviTurnClick() {

    }

    @Override
    public void onNextRoadClick() {

    }

    @Override
    public void onScanViewButtonClick() {

    }

    @Override
    public void onLockMap(boolean b) {
        //锁地图状态发生变化时回调

    }

    @Override
    public void onNaviViewLoaded() {
        Log.d("wlx", "导航页面加载成功");
        Log.d("wlx", "请不要使用AMapNaviView.getMap().setOnMapLoadedListener();会overwrite导航SDK内部画线逻辑");

    }
}

